//
//  SeleTimePickerView.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/22.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class SeleTimePickerView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    var backSeletime:BackStringValue!
    
    var pickerView:UIPickerView!
    
    var titleArray = [String]()
    
    var effect:EffectView!
    
    var shadowLayer:CALayer!
    
    var contentView:UIView!
    
    var seleTime = ""
    
    var titleLabel:UILabel!
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        effect = EffectView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        effect.alpha = 0
        self.addSubview(self.effect)
        effect.hidenClosure = { [weak self]  in
            self?.hidenPickerView()
        }
        
        self.contentView = UIView.init(frame: CGRect.init(x: 0, y: self.height, width: self.width, height: 170 + (Utils.isIphoneX() ? 34 : 0)))
        self.contentView.backgroundColor = UIColor.clear
        self.addSubview(self.contentView)
        
        shadowLayer = CALayer.init()
        shadowLayer.frame = CGRect.init(x: 0, y:0 , width: self.contentView.width, height: self.contentView.height)
        shadowLayer.cornerRadius = 20
        shadowLayer.backgroundColor = UIColor.white.cgColor
        shadowLayer.shadowColor = UIColor.init(hexColor: "#000000").cgColor
        shadowLayer.shadowOffset = CGSize.init(width: 0, height: -10)
        shadowLayer.shadowRadius = 5
        shadowLayer.shadowOpacity = 0.08
        self.contentView.layer.addSublayer(shadowLayer)
        
        let cancelBtn = Utils.buttonWith(frame: CGRect.init(x: 12, y: 0, width: 50, height: 43.5), font: UIFont.systemFont(ofSize: 14), textColor: "#999999", title: "取消", bgColor: nil)
        cancelBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        self.contentView.addSubview(cancelBtn)
        
        let confirmBtn = Utils.buttonWith(frame: CGRect.init(x: UIScreen.ScreenWidth - 50 - 12, y: 0, width: 50, height: 43.5), font: UIFont.systemFont(ofSize: 14), textColor: "#f7c201", title: "确定", bgColor: nil)
        confirmBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        self.contentView.addSubview(confirmBtn)
        
        titleLabel = Utils.labelWith(frame: CGRect.init(x: cancelBtn.right, y: 0, width: UIScreen.ScreenWidth - cancelBtn.right - confirmBtn.width - 12, height: 43.5), font: UIFont.boldSystemFont(ofSize: 14), textAlignment: .center, textColor: "#000000", text: "")
        self.contentView.addSubview(titleLabel)
        
        self.pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: cancelBtn.bottom, width: UIScreen.ScreenWidth, height: self.contentView.height - 43.5 - (Utils.isIphoneX() ? 34 : 0)))
        self.pickerView.delegate = self
        self.pickerView.dataSource = self
        self.pickerView.backgroundColor = UIColor.white
        
        self.contentView.addSubview(self.pickerView)
        
    }
    
    @objc func btnClick(sender:UIButton){
        if sender.titleLabel?.text == "取消" {
            self.hidenPickerView()
        }else{
            if self.backSeletime != nil {
                self.backSeletime(self.seleTime)
            }
            self.hidenPickerView()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reloadAll(titleArray:[String],andTitle title:String){
        self.titleLabel.text = title
        self.titleArray = titleArray
        self.seleTime = titleArray.first!
        self.pickerView.reloadAllComponents()
    }
    
    func show(){
        self.isHidden = false
        UIView.animate(withDuration: 0.3, animations: {
            self.effect.alpha = 1
        }) { (_) in
            self.contentView.top = UIScreen.ScreenHeight - 170 - (Utils.isIphoneX() ? 34 : 0)
        }
    }
    
    func hidenPickerView(){
        UIView.animate(withDuration: 0.2, animations: {
            self.contentView.top = UIScreen.ScreenHeight
        }) { (_) in
            self.effect.alpha = 0
            self.isHidden = true
        }
    }
    
}


extension SeleTimePickerView:UIPickerViewDelegate,UIPickerViewDataSource{
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.titleArray.count
    }
    
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//
//
//
//        return self.titleArray[row]
//    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.seleTime = self.titleArray[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        
        // 显示pickder的双横线
        for view in pickerView.subviews {
            if view.height <= 1 {
                view.isHidden = false
                view.height = 0.5
                view.backgroundColor = UIColor.init(hexColor: "#dedede")
            }
        }
        
        let label = UILabel()
        label.textColor = UIColor.init(hexColor: "#333333")
        label.font = UIFont.systemFont(ofSize: 15)
        label.text = self.titleArray[row]
        label.sizeToFit()
        return label
    }
}














